package com.mooc.biz.impl;

import com.mooc.biz.MusicBiz;
import com.mooc.entity.Music;
import com.mooc.mapper.MusicMapper;
import com.mooc.util.SnowFlake;
import com.mooc.util.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service(value="MusicBiz")
public class MusicBizImpl implements MusicBiz{

    SnowFlake snowFlake = new SnowFlake(2, 3);
	@Autowired
	MusicMapper musicMapper;
	@Override
	public List<Music> selectAllMusic() {
		// TODO Auto-generated method stub
		return musicMapper.selectAllMusic();
	}
	@Override
	public Music selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return musicMapper.selectByPrimaryKey(id);
	}
	@Override
	public int updateByPrimaryKeySelective(Music record) {
		// TODO Auto-generated method stub
		return musicMapper.updateByPrimaryKeySelective(record);
	}
	@Override
	public List<Music> musicsearch(String search) {
		// TODO Auto-generated method stub
		return musicMapper.musicsearch(search);
	}
	@Override
	public List<Music> freeMusic() {
		// TODO Auto-generated method stub
		return musicMapper.freemusic();
	}
	@Override
	public List<Music> vipMusic() {
		// TODO Auto-generated method stub
		return musicMapper.vipmusic();
	}
	@Override
	public int deleteByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return musicMapper.deleteByPrimaryKey(id);
	}
	@Override
	public int savemusic(HttpServletRequest req) {
		// TODO Auto-generated method stub
        long l = snowFlake.nextId();
        Music music =  UploadFile.uploadFile(String.valueOf(l), req,Music.class);
		String mp3 = music.getMp3();
		if (mp3 != null) {
			music.setMp3("music/"+mp3+".mp3");
		}
		if(music.getId()==null||music.getId()==0) {
			music.setId(l);
			musicMapper.insertSelective(music);
			/*System.out.println(music.getId());*/
		}else {
			musicMapper.updateByPrimaryKeySelective(music);
		}
		return 0;
	}

}
